package com.hutiao.snowhb.actions.wechat;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.hutiao.snowhb.common.SimsimiClient;
import com.hutiao.snowhb.wechat.tools.WeiXinTools;
import com.hutiao.snowhb.wechat.vo.WxRecvEventClick;
import com.hutiao.snowhb.wechat.vo.WxRecvEventGeo;
import com.hutiao.snowhb.wechat.vo.WxRecvEventSubscribe;
import com.hutiao.snowhb.wechat.vo.WxRecvEventUnsubscribe;
import com.hutiao.snowhb.wechat.vo.WxRecvGeoMsg;
import com.hutiao.snowhb.wechat.vo.WxRecvLinkMsg;
import com.hutiao.snowhb.wechat.vo.WxRecvMsg;
import com.hutiao.snowhb.wechat.vo.WxRecvPicMsg;
import com.hutiao.snowhb.wechat.vo.WxRecvVideoMsg;
import com.hutiao.snowhb.wechat.vo.WxRecvVoiceMsg;
import com.hutiao.snowhb.wechat.vo.WxRecvTextMsg;
import com.opensymphony.xwork2.ActionSupport;

@Namespace("/wechat")
public class WechatAction extends ActionSupport implements ServletRequestAware {
	
	private static Logger logger = Logger.getLogger(WechatAction.class);
	
	private static final long serialVersionUID = -2413791342162564357L;
	
	private String result = "";

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
	private HttpServletRequest request;

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Action(value="index", results = {
			@Result(name="success",type="xmlstr", params={"stringName", "result"}),
			@Result(name="rn_check", type="chain", location="check"),
			@Result(name="rn_textmsg", type="chain", location="handletextmsg"),
			@Result(name="rn_picmsg", type="chain", location="handlepicmsg"),
			@Result(name="rn_voicemsg", type="chain", location="handlevoicemsg"),
			@Result(name="rn_videomsg", type="chain", location="handlevideomsg"),
			@Result(name="rn_geomsg", type="chain", location="handlegeomsg"),
			@Result(name="rn_linkmsg", type="chain", location="handlelinkmsg"),
			@Result(name="rn_eventmsg", type="chain", location="handleeventmsg"),
			@Result(name="rn_eventgeo", type="chain", location="handleeventgeo")})
	public String execute() throws Exception {
		
		String method = request.getMethod().toUpperCase();
		logger.info(method);
		
		if (method.equals("GET")) {
			// 跳转到验证Action 
			return "rn_check";
		}
		
		if (!method.equals("POST")) {
			// 不处理非GET、非POST请求
			this.result = "error";
			return SUCCESS;
		}
		
		String wxMsgXml = IOUtils.toString(request.getInputStream(),"utf-8");
		logger.info(wxMsgXml);
		WxRecvMsg msg = WeiXinTools.recv(wxMsgXml);
		request.setAttribute("recvMsg", msg);
		
		if (msg instanceof WxRecvTextMsg) {
			return "rn_textmsg";
		} else if (msg instanceof WxRecvPicMsg) {
			return "rn_picmsg";
		} else if (msg instanceof WxRecvVoiceMsg) {
			return "rn_voicemsg";
		} else if (msg instanceof WxRecvVideoMsg) {
			return "rn_videomsg";
		} else if (msg instanceof WxRecvGeoMsg) {
			return "rn_geomsg";
		} else if (msg instanceof WxRecvLinkMsg) {
			return "rn_linkmsg";
		} else if (msg instanceof WxRecvEventSubscribe) {	// 关注事件,扫描二维码事件
			return "rn_eventmsg";
		} else if (msg instanceof WxRecvEventClick) {
			return "rn_eventmsg";
		} else if (msg instanceof WxRecvEventGeo) {
			return "rn_eventgeo";
		}
		
		return SUCCESS;
	}
	

}
